package com.memo.receiver;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.memo.util.SmsSender;

public class AlarmReceiver extends BroadcastReceiver {
	private final String REMINDER_BUNDLE = "MyReminderBundle";
	private final String EXT_PHONE = "phone";
	private final String EXT_MESSAGE = "message";
	// this constructor is called by the alarm manager.
	public AlarmReceiver() {
	}

	public AlarmReceiver(Context context, Bundle extras, int timeoutInSeconds) {
		AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(context, AlarmReceiver.class);
		intent.putExtra(REMINDER_BUNDLE, extras);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		Calendar time = Calendar.getInstance();
		time.setTimeInMillis(System.currentTimeMillis());
		time.add(Calendar.SECOND, timeoutInSeconds);
		alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		(new SmsSender()).sendMessage(context, intent.getStringExtra(EXT_PHONE), intent.getStringExtra(EXT_MESSAGE)); 
	}
}
